package org.wuzhou.lvyou.base;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.util.DisplayMetrics;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;

public abstract class BaseView extends SurfaceView implements Callback,
		Runnable {

	private SurfaceHolder surfaceHolder;// 定义画布持有者

	private Canvas canvas; // 定义画布

	DisplayMetrics dm;

	// 定义存储屏幕的分辨率变量
	protected int screenW, screenH;

	public BaseView(Context context) {
		super(context);
		dm = getResources().getDisplayMetrics();
		surfaceHolder = getHolder();
		surfaceHolder.addCallback(this);

		// 获取屏幕分辨率
		screenW = dm.widthPixels;
		screenH = dm.heightPixels;

		// 设置屏幕常亮
		this.setKeepScreenOn(true);

		initBimtap();
		initRect();

	}

	public abstract void initRect();

	public abstract void initBimtap();

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		new Thread(this).start();
	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {

	}

	@Override
	public void run() {

		while (true) {
			try {
				canvas = surfaceHolder.lockCanvas();
				synchronized (surfaceHolder) {
					this.onDraw(canvas);
				}
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				if (canvas != null) {
					surfaceHolder.unlockCanvasAndPost(canvas);
				}
			}
			try {
				Thread.sleep(50);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);

		// 清屏
		canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);

	}

}
